/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.notaria.guerrasalas.bc;

import com.notaria.guerrasalas.be.Igv;
import gob.satt.comun.jpa.BadUpdateException;
import gob.satt.comun.jpa.NotFoundException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Emmanuel
 */
public class IGVDelegate extends AbstractDelegate{

    private GuerraSalasServiceLocal local;

    public IGVDelegate() {
        local = lookupGuerraSalasServiceBean();
    }

    public void crear(Igv igv) throws BadUpdateException{
        try {
            local.create(igv);
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new BadUpdateException("Crear IGV. "+getMensajeError(e),e);
        }
    }

    public void darDeBaja(Igv igv) throws BadUpdateException{
        try {
            igv.setFechaBaja(new Date());
            local.edit(igv);
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new BadUpdateException("Dar de baja IGV. "+getMensajeError(e),e);
        }
    }

    public List<Igv> listarIGV() throws NotFoundException{
        try {
            return (List<Igv>)local.execute(new ListarIGVCommand());
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Listar IGV. "+getMensajeError(e),e);
        }
    }

    public Igv obtenerIGVActual() throws NotFoundException{
        try {
            return (Igv)local.execute(new ObtenerIGVActualCommand());
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Obtener IGV Actual. "+getMensajeError(e),e);
        }
    }
    
    private GuerraSalasServiceLocal lookupGuerraSalasServiceBean() {
        try {
            Context c = new InitialContext();
            return (GuerraSalasServiceLocal) c.lookup("java:comp/env/GuerraSalasServiceBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
